package com.estar.platform.version.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.UploadFile;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDocument;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.pojo.base.TSTypegroup;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.estar.platform.version.entity.TSVersionAnnexInfoEntity;
import com.estar.platform.version.entity.TSVersionEntity;
import com.estar.platform.version.service.SysVerServiceI;

/**   
 * @Title: Controller
 * @Description: 版本管理
 * @author chenxinan
 * @date 2016-07-11 15:48:50
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/sysVerController")
public class SysVerController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SysVerController.class);

	@Autowired
	private SysVerServiceI sysVerService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 用户信息列表 页面跳转
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("gdia/version/sysVerList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(TSVersionEntity tSysVer,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(TSVersionEntity.class, dataGrid);
		//查询条件组装器
		HqlGenerateUtil.installHql(cq, tSysVer, request.getParameterMap());
		this.sysVerService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除用户信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(TSVersionEntity tSysVer, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		tSysVer = systemService.getEntity(TSVersionEntity.class, tSysVer.getId());
		message = "用户信息删除成功";
		sysVerService.delete(tSysVer);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加用户信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(TSVersionEntity tSysVer, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(tSysVer.getId())) {
			message = "版本信息更新成功";
			TSVersionEntity t = sysVerService.get(TSVersionEntity.class, tSysVer.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(tSysVer, t);
				sysVerService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "版本信息更新失败";
			}
		} else {
			message = "版本信息添加成功";
			sysVerService.save(tSysVer);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 用户信息列表页面跳转
	 * 
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(TSVersionEntity tSysVer, HttpServletRequest req) throws ParseException {
		if (StringUtil.isNotEmpty(tSysVer.getId())) {
			tSysVer = sysVerService.getEntity(TSVersionEntity.class, tSysVer.getId());
			req.setAttribute("tSysVerPage", tSysVer);
		}else{
			tSysVer = new TSVersionEntity();
			tSysVer.setSysCde("gaii"); 
			tSysVer.setSysNme("广东保协");
			tSysVer.setFileSize("1024");
			tSysVer.setOperateTm(new Date());
			req.setAttribute("tSysVerPage", tSysVer);
		}
		return new ModelAndView("gdia/version/sysVer");
	}
	
	/**
	 * 保存文件
	 * @param document
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(params = "saveFiles", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson saveFiles(HttpServletRequest request, HttpServletResponse response, TSDocument document) {
		AjaxJson j = new AjaxJson();
		Map<String, Object> attributes = new HashMap<String, Object>();
		TSTypegroup tsTypegroup=systemService.getTypeGroup("fieltype","文档分类");
		TSType tsType = systemService.getType("files","附件", tsTypegroup);
		String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID
		String documentTitle = oConvertUtils.getString(request.getParameter("documentTitle"));// 文件标题
		if (StringUtil.isNotEmpty(fileKey)) {
			document.setId(fileKey);
			document = systemService.getEntity(TSDocument.class, fileKey);
			document.setDocumentTitle(documentTitle);

		}
		document.setSubclassname(MyClassLoader.getPackPath(document));
		document.setCreatedate(DateUtils.gettimestamp());
		document.setTSType(tsType);
		UploadFile uploadFile = new UploadFile(request, document);
		uploadFile.setCusPath("files");
		uploadFile.setSwfpath("swfpath");
		document = systemService.uploadFile(uploadFile);
		attributes.put("url", document.getRealpath());
		attributes.put("fileKey", document.getId());
		attributes.put("name", document.getAttachmenttitle());
		attributes.put("viewhref", "commonController.do?objfileList&fileKey=" + document.getId());
		attributes.put("delurl", "commonController.do?delObjFile&fileKey=" + document.getId());
		j.setMsg("文件添加成功");
		j.setAttributes(attributes);
		return j;
	}
}
